package com.hnust.root.web.controller.forward.user;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.common.base.response.PageData;
import com.hnust.root.dao.entity.domain.forward.Comment;
import com.hnust.root.dao.entity.domain.forward.Reply;
import com.hnust.root.dao.entity.domain.forward.UserMessage;
import com.hnust.root.service.forword.wx.CommentService;
import com.hnust.root.service.forword.wx.MessageService;
import com.hnust.root.service.forword.wx.ReplyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/group")
public class CommentController extends BaseRestController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private ReplyService replyService;

    @Autowired
    private MessageService messageService;

    @ApiOperation("通过话题ID获取评论列表")
    @PostMapping("/getCommentList")
    public BaseResult<PageData> getCommentList(@RequestBody JSONObject jsonObject) {
        int topicId = jsonObject.getInt("id");
        int pageNum = jsonObject.getInt("start");
        int pageSize = jsonObject.getInt("limit");
        IPage<Comment> page = new Page<>(pageNum, pageSize);
        page = commentService.getCommentByTopicId(page, topicId);
        return fillSuccessData(new PageData(page));
    }

    @ApiOperation("添加评论")
    @PostMapping("/addComment")
    public BaseResult addComment(@RequestBody Comment comment) {
        String openId = getWxAccountOpenId();
        comment.setOpenId(openId);
        comment.setCreateDate(new Date());
        boolean res = commentService.addComment(comment);
        return res ? fillSuccessData() : fillFailData();
    }


    @ApiOperation("回复话题")
    @PostMapping("/addReply")
    public BaseResult addReply(@RequestBody JSONObject jsonObject) {
        String fromUserOpenId = getWxAccountOpenId();
        Reply reply = new Reply();
        UserMessage userMessage = new UserMessage();

        int topicId = jsonObject.getInt("topicId");
        //类型：回复，评论
        String rType = jsonObject.getStr("type");
        if (rType.equals("comment")) {
            //说明是评论
            int commentId = jsonObject.getInt("commentId");
            reply.setCommentId(commentId);
            Comment comment = commentService.getCommentById(commentId);
            userMessage.setToOpenId(comment.getOpenId());
        } else {
            //说明是回复
            int replyId = jsonObject.getInt("replyId");
            reply.setReplyId(replyId);
            Reply originReply = replyService.getReplyById(replyId);
            reply.setCommentId(originReply.getCommentId());
            userMessage.setToOpenId(originReply.getFromUser());
        }
        reply.setContent(jsonObject.getStr("content"));
        reply.setFromUser(fromUserOpenId);
        reply.setTopicId(topicId);

        boolean res =  replyService.addReply(reply);

        //添加用户消息
        userMessage.setTopicId(topicId);
        userMessage.setFromOpenId(fromUserOpenId);
        userMessage.setFlag(false);
        userMessage.setUpdatedAt(new Date());
        userMessage.setContentType(rType);

        messageService.addUserMessage(userMessage);
        return res ? fillSuccessData() : fillFailData();
    }

    @ApiOperation("获取评论列表")
    @PostMapping("/getReplyList")
    public BaseResult<List<Reply>> getReplyList(@RequestBody JSONObject jsonObject) {
        int commentId = jsonObject.getInt("cid");
        return fillSuccessData(replyService.getReplyList(commentId));
    }

}
